﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;

public partial class other_Default : KPage
{
   protected void Page_Load(object sender, EventArgs e)
    {

    }

   #region Common

   [WebMethod]
   public static string EnuLovsParse(List<string> list)
   {
       string ParseMethod = Convert.ToString(list[0]);
       string ParseValue = Convert.ToString(list[1]);
       string jsonData = "";
       try
       {
           
           //Customer c = new Customer();
           //CustomerCollection cc = new CustomerCollection();
           //cc = c.SearchCustomerByName(KeyWord);
           //JsonSerializerSettings nJSS = new JsonSerializerSettings();
           //nJSS.Converters.Add(new DataTableConverter());
           //jsonData = JsonConvert.SerializeObject(cc, Formatting.None, nJSS);
       }
       catch (Exception ex)
       {
           jsonData = "0";
       }
       return (jsonData);
   }

   #endregion

   #region List

   [WebMethod]
    public static string SearchCustomer(List<string> list)
    {
        string KeyWord = Convert.ToString(list[0]);
        string jsonData = "";
        try
        {
            Customer c = new Customer();
            CustomerCollection cc = new CustomerCollection();
            cc = c.SearchCustomerByName(KeyWord);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(cc, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }
    
    [WebMethod]
    public static string ListCustomer()
    {
        string jsonData = "";
        try
        {
            Customer c = new Customer();
            CustomerCollection cc = new CustomerCollection();
            cc = c.GetAllCustomers(true);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(cc, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string GetCustomerDetail(List<string> list)
    {
        int CustomerId = Convert.ToInt32(list[0]);
        string jsonData = "";
        try
        {
            Customer c = new Customer();
            c = c.GetCustomerById(CustomerId);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(c, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);


    }

    [WebMethod]
    public static string SearchClient(List<string> list)
    {
        string KeyWord = Convert.ToString(list[0]);
        string jsonData = "";
        try
        {
            Client cl = new Client();
            ClientCollection clc = new ClientCollection();
            clc = cl.SearchClientByName(KeyWord);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(clc, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }
   
    [WebMethod]
    public static string ListCustomerClients(List<string> list)
    {
        int CustomerId = Convert.ToInt32(list[0]);
        string jsonData = "";
        try
        {
            Customer c = new Customer();
            c = c.GetCustomerById(CustomerId);
            ClientCollection ccl = new ClientCollection();
            ccl = c.GetAllClients(true);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(ccl, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string GetClientDetail(List<string> list)
    {
        int ClientId = Convert.ToInt32(list[0]);
        string jsonData = "";
        try
        {
            Client c = new Client();
            c = c.GetClientById(ClientId);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(c, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);


    }

    [WebMethod]
    public static string GetClientWFList(List<string> list)
    {
        int ClientId = Convert.ToInt32(list[0]);
        string jsonData = "";
        try
        {
            WF wf = new WF();
            DataTable wfList = wf.ListWorkflowsForClient(ClientId);
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(wfList, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string GetWfItemList(List<string> list)
    {
        int ClientId = Convert.ToInt32(list[0]);
        int WfId = Convert.ToInt32(list[1]);
        string jsonData = "";
        try
        {
            WF wf = new WF();
            List<string> wfItemList = new List<string>();
            wfItemList.Add("Document Request");
            wfItemList.Add("Document Receival");
            wfItemList.Add("Appointment Schedule");
            wfItemList.Add("Request Required Documents for Appointment");
            wfItemList.Add("Appointment Required Document Receival");
            wfItemList.Add("Waiting for Appointment");
            wfItemList.Add("Visit for Appointment");
            wfItemList.Add("Request Fee Assesment");
            wfItemList.Add("Waiting for Delivery Note");
            wfItemList.Add("Residance Permit Receival");
            wfItemList.Add("Request Residance Permit Copy");
            
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(wfItemList);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string ListLOV(List<string> list)
    {
        int CustomerOrClientId = Convert.ToInt32(list[0]);
        string RecordType = Convert.ToString(list[1]);
        string RecordName = Convert.ToString(list[2]);
        string LoadOption = Convert.ToString(list[3]);
        if (RecordName == "") RecordName = null;
        ENU.CCLoVRecordName ParsedRecordName = ENU.RecordNameParse(RecordName);
        if (RecordType == "") RecordType = null;
        ENU.CCLoVRecordType ParsedRecordType = ENU.RecordTypeParse(RecordType);
        ENU.CCLovLoadOption ParsedLoadOption = ENU.LoadOptionParse(LoadOption);
        string jsonData = "";
        try
        {
            CustomerClientLoV lov = new CustomerClientLoV();
            CustomerClientLoVCollection lovc = lov.LoadAll(CustomerOrClientId, ENU.Entity.Customer, ParsedLoadOption, ParsedRecordType, ParsedRecordName);
            List<CustomerClientLoV> LOVList = lovc.ToList();
            jsonData = JsonConvert.SerializeObject(LOVList, Formatting.None);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string ListRecordType()
    {
        string jsonData = "";
        try
        {
            DataTable rtt = ENU.EnumToDataTable(typeof(ENU.CCLoVRecordType), "KF", "VF");
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(rtt, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string ListRecordName()
    {
        //int RecordType = Convert.ToInt32(list[0]);
        string jsonData = "";
        try
        {
            DataTable rtt = ENU.EnumToDataTable(typeof(ENU.CCLoVRecordName), "KF", "VF");
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(rtt, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    [WebMethod]
    public static string ListRequestType()
    {
        //int RecordType = Convert.ToInt32(list[0]);
        string jsonData = "";
        try
        {
            DataTable rtt = ENU.EnumToDataTable(typeof(ENU.WFCode), "KF", "VF");
            JsonSerializerSettings nJSS = new JsonSerializerSettings();
            nJSS.Converters.Add(new DataTableConverter());
            jsonData = JsonConvert.SerializeObject(rtt, Formatting.None, nJSS);
        }
        catch (Exception ex)
        {
            jsonData = "0";
        }
        return (jsonData);
    }

    #endregion

    #region Update

    [WebMethod]
    public static string UpdateCustomer(List<string> list)
    {
        int CustomerId = Convert.ToInt32(list[0]);
        string CustomerName = Convert.ToString(list[1]);
        string result = "";
        try
        {
            Customer c = new Customer(CustomerId, CustomerName, ENU.ENTOperation.Update, "admin");
            Exception ex = new Exception();
            c.Save(ref ex);
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string UpdateClient(List<string> list)
    {
        int ClientId = Convert.ToInt32(list[0]);
        string ClientName = Convert.ToString(list[1]);
        int CustomerId = Convert.ToInt32(list[2]);
        string result = "";
        try
        {
            Client c = new Client(ClientId, ClientName, CustomerId, ENU.ENTOperation.Update, "admin");
            Exception ex = new Exception();
            c.Save(ref ex);
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string UpdateLovs(List<string> list)
    {
        int CustomerOrClientId = Convert.ToInt32(list[0]);
        int RecordType = Convert.ToInt32(list[1]);
        int RecordName = Convert.ToInt32(list[2]);
        string RecordDescription = Convert.ToString(list[3]);
        string RecordValue = Convert.ToString(list[4]);
        int RecordId = Convert.ToInt32(list[5]);
        string result = "";
        try
        {
            CustomerClientLoV ccl = new CustomerClientLoV();
            ccl.CustomerOrClientId = CustomerOrClientId;
            ccl.RecId = RecordId;
            ccl.RecordType = ENU.RecordTypeParse(RecordType);
            ccl.RecordName = ENU.RecordNameParse(RecordName);
            ccl.Description = RecordDescription;
            ccl.RecordValue = RecordValue;
            ccl.RecStatus = ENU.RecStatus.Modified;
            ccl.LastSavedBy = "admin";
            ccl.Save();
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }
    
    #endregion

    #region Insert

    [WebMethod]
    public static string InsertCustomer(List<string> list)
    {
        string CustomerName = Convert.ToString(list[0]);
        string CustomerCode = Convert.ToString(list[1]);
        string result = "";
        try
        {
            Customer cs = new Customer(CustomerCode, CustomerName, "admin");
            Exception ex = new Exception();
            if (cs.Save(ref ex))
            {
                result = cs.CustomerId.ToString();
            }

        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string AddLovs(List<string> list)
    {
        int CustomerOrClientId = Convert.ToInt32(list[0]);
        int RecordType = Convert.ToInt32(list[1]);
        int RecordName = Convert.ToInt32(list[2]);
        string RecordDescription = Convert.ToString(list[3]);
        string RecordValue = Convert.ToString(list[4]);
        string CustomerOrClient = Convert.ToString(list[5]);
        string result = "";
        try
        {
            CustomerClientLoV ccl = new CustomerClientLoV();
            ccl.CustomerOrClientId = CustomerOrClientId;
            ccl.RecordType = ENU.RecordTypeParse(RecordType);
            ccl.RecordName = ENU.RecordNameParse(RecordName);
            ccl.Description = RecordDescription;
            ccl.RecordValue = RecordValue;
            ccl.RecStatus = ENU.RecStatus.Added;
            ccl.LastSavedBy = "admin";
            if (CustomerOrClient == "Customer") ccl.CustomerOrClient = ENU.Entity.Customer;
            else ccl.CustomerOrClient = ENU.Entity.Client;
            ccl.Save();
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string InsertClient(List<string> list)
    {
        int CustomerId = Convert.ToInt32(list[0]);
        string ClientName = Convert.ToString(list[1]);
        string ClientCode = Convert.ToString(list[2]);
        string result = "";
        try
        {
            Client cl = new Client(ClientCode, ClientName, "", CustomerId);            
            Exception ex = new Exception();
            if (cl.Save(ref ex))
            {
                result = cl.ClientId.ToString();
            }

        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string AddRequest(List<string> list)
    {
        int ClientId = Convert.ToInt32(list[0]);
        int RequestType = Convert.ToInt32(list[1]);        
        //Customer Id'ye ihtiyaç yok
        Client c = new Client();
        c = c.GetClientById(ClientId);
        int CustomerId = c.Customer.CustomerId.Value;
        //*************
        string result = "";
        try
        {          
            ERR.UIException ex = new ERR.UIException();
            WF.StartWorkflow(ENU.WFCodeParse(RequestType), CustomerId, ClientId, ref ex);
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    #endregion

    #region Delete

    [WebMethod]
    public static string DeleteCustomer(List<string> list)
    {
        int CustomerId = Convert.ToInt32(list[0]);
        string result = "";
        try
        {
            Customer c = new Customer(CustomerId, "test", ENU.ENTOperation.Delete, "admin");
            c.CustomerName = "test";
            Exception ex = new Exception();
            c.Save(ref ex);
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string DeleteClient(List<string> list)
    {
        int ClientId = Convert.ToInt32(list[0]);
        //*******************************************Delete için customerId null geçilebilmeli mi?
        int CustomerId = 1;
        string result = "";
        try
        {
            Client c = new Client(ClientId, "test", CustomerId, ENU.ENTOperation.Delete, "admin");
            c.ClientName = "test";
            Exception ex = new Exception();
            c.Save(ref ex);
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    [WebMethod]
    public static string DeleteLovs(List<string> list)
    {
        int CustomerOrClientId = Convert.ToInt32(list[0]);
        int RecordId = Convert.ToInt32(list[1]);
        string result = "";
        try
        {
            CustomerClientLoV ccl = new CustomerClientLoV();
            ccl.CustomerOrClientId = CustomerOrClientId;
            ccl.RecId = RecordId;
            ccl.RecordType = 0;
            ccl.RecordName = 0;
            ccl.Description = "";
            ccl.RecordValue = ""; ;
            ccl.RecStatus = ENU.RecStatus.Deleted;
            ccl.LastSavedBy = "admin";
            ccl.Save();
            result = "1";
        }
        catch (Exception ex)
        {
            result = "0";
        }
        return (result);
    }

    #endregion









    protected void LinkButton_LogOut_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("Login.aspx");
    }
}
